package com.eshop.service.Impl;

import com.eshop.dao.AddressDao;
import com.eshop.dao.Impl.AddressDaoImpl;
import com.eshop.dao.Impl.UserDaoImpl;
import com.eshop.dao.UserDao;
import com.eshop.model.Address;
import com.eshop.model.User;
import com.eshop.service.UserCenterService;

import java.util.List;

public class UserCenterServiceImpl implements UserCenterService {
    UserDao userDao = new UserDaoImpl();
    AddressDao addressDao = new AddressDaoImpl();

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public List<Address> getAddressByUserId(int userId) {
        return addressDao.queryAddressByUserId(userId);
    }

    @Override
    public Address getAddressByAddressId(int addressId) {
        return addressDao.queryAddressByAddressId(addressId);
    }

    @Override
    public void addAddress(Address address) {
        addressDao.addAddress(address);
    }

    @Override
    public void deleteAddressByAddressId(int addressID) {
        addressDao.deleteAddressByAddressId(addressID);
    }

    @Override
    public void updateAddress(Address address) {
        addressDao.updateAddress(address);
    }

    @Override
    public Address getAddressByUserIdAndDefault(int userId, int isDefault) {
        return addressDao.queryAddressByUserIdAndDefault(userId,isDefault);
    }

    @Override
    public void updateDefaultAddress(int userId, int addressId) {
        Address defaultAddress = addressDao.queryAddressByUserIdAndDefault(userId,1);
        if (defaultAddress != null){
            defaultAddress.setAddr_isdefault(0);
            addressDao.updateAddress(defaultAddress);
        }

        Address newDefaultAddress = addressDao.queryAddressByAddressId(addressId);
        newDefaultAddress.setAddr_isdefault(1);
        addressDao.updateAddress(newDefaultAddress);
    }

}
